package cn.iocoder.yudao.module.crossborder.platform.service.provider.amazon.client;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * Amazon 模拟数据生成器
 * 生成符合Amazon SP-API格式的模拟数据
 *
 * @author 芋道源码
 */
@Component
@Slf4j
public class AmazonMockDataGenerator {

    private final Random random = new Random();
    private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");

    // 模拟商品名称
    private final String[] PRODUCT_NAMES = {
        "Wireless Bluetooth Headphones", "Smart Phone Case", "USB-C Cable", "Portable Charger",
        "Bluetooth Speaker", "Laptop Stand", "Wireless Mouse", "Keyboard Cover",
        "Phone Screen Protector", "Car Phone Mount", "Tablet Case", "Power Bank",
        "Bluetooth Earbuds", "Phone Ring Holder", "Laptop Bag", "USB Hub"
    };

    // 模拟品牌
    private final String[] BRANDS = {
        "TechPro", "SmartGear", "ElectroMax", "DigitalPlus", "PowerTech",
        "ConnectWise", "GadgetMaster", "TechSavvy", "InnovateTech", "FutureTech"
    };

    // 模拟分类
    private final String[] CATEGORIES = {
        "Electronics", "Cell Phones & Accessories", "Computers & Accessories",
        "Home & Kitchen", "Sports & Outdoors", "Automotive", "Health & Personal Care"
    };

    // 模拟颜色
    private final String[] COLORS = {
        "Black", "White", "Blue", "Red", "Silver", "Gold", "Gray", "Pink", "Green", "Purple"
    };

    // 模拟尺寸
    private final String[] SIZES = {
        "Small", "Medium", "Large", "X-Large", "One Size", "Universal"
    };

    /**
     * 生成随机ASIN
     */
    public String generateAsin() {
        return "B" + String.format("%09d", random.nextInt(1000000000));
    }

    /**
     * 生成随机SKU
     */
    public String generateSku() {
        return "SKU-" + String.format("%06d", random.nextInt(1000000));
    }

    /**
     * 生成随机订单ID
     */
    public String generateOrderId() {
        return "MOCK-ORDER-" + System.currentTimeMillis() + "-" + random.nextInt(1000);
    }

    /**
     * 生成随机商品名称
     */
    public String generateProductName() {
        return PRODUCT_NAMES[random.nextInt(PRODUCT_NAMES.length)];
    }

    /**
     * 生成随机品牌
     */
    public String generateBrand() {
        return BRANDS[random.nextInt(BRANDS.length)];
    }

    /**
     * 生成随机分类
     */
    public String generateCategory() {
        return CATEGORIES[random.nextInt(CATEGORIES.length)];
    }

    /**
     * 生成随机颜色
     */
    public String generateColor() {
        return COLORS[random.nextInt(COLORS.length)];
    }

    /**
     * 生成随机尺寸
     */
    public String generateSize() {
        return SIZES[random.nextInt(SIZES.length)];
    }

    /**
     * 生成随机价格
     */
    public BigDecimal generatePrice(double min, double max) {
        double price = min + (max - min) * random.nextDouble();
        return BigDecimal.valueOf(price).setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 生成随机数量
     */
    public int generateQuantity(int min, int max) {
        return random.nextInt(max - min + 1) + min;
    }

    /**
     * 生成随机重量
     */
    public BigDecimal generateWeight() {
        double weight = 0.1 + 10.0 * random.nextDouble(); // 0.1 - 10.1 pounds
        return BigDecimal.valueOf(weight).setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 生成随机尺寸
     */
    public Map<String, BigDecimal> generateDimensions() {
        Map<String, BigDecimal> dimensions = new HashMap<>();
        dimensions.put("length", BigDecimal.valueOf(1 + 20 * random.nextDouble()).setScale(2, RoundingMode.HALF_UP));
        dimensions.put("width", BigDecimal.valueOf(1 + 15 * random.nextDouble()).setScale(2, RoundingMode.HALF_UP));
        dimensions.put("height", BigDecimal.valueOf(1 + 10 * random.nextDouble()).setScale(2, RoundingMode.HALF_UP));
        return dimensions;
    }

    /**
     * 生成随机日期时间
     */
    public String generateDateTime(int daysAgo) {
        LocalDateTime dateTime = LocalDateTime.now().minusDays(random.nextInt(daysAgo + 1));
        return dateTime.format(dateTimeFormatter);
    }

    /**
     * 生成随机订单状态
     */
    public String generateOrderStatus() {
        String[] statuses = {"Pending", "Unshipped", "PartiallyShipped", "Shipped", "Canceled", "Unfulfillable"};
        return statuses[random.nextInt(statuses.length)];
    }

    /**
     * 生成随机履行渠道
     */
    public String generateFulfillmentChannel() {
        return random.nextBoolean() ? "MFN" : "AFN"; // Merchant Fulfilled Network or Amazon Fulfilled Network
    }

    /**
     * 生成随机市场ID
     */
    public String generateMarketplaceId() {
        String[] marketplaceIds = {
            "ATVPDKIKX0DER", // US
            "A2EUQ1WTGCTBG2", // Canada
            "A1AM78C64UM0Y8", // Mexico
            "A1PA6795UKMFR9", // Germany
            "A1RKKUPIHCS9HS", // Spain
            "A13V1IB3VIYZZH", // France
            "APJ6JRA9NG5V4", // Italy
            "A1F83G8C2ARO7P", // UK
            "A21TJRUUN4KGV", // India
            "A1VC38T7YXB528" // Japan
        };
        return marketplaceIds[random.nextInt(marketplaceIds.length)];
    }

    /**
     * 生成随机客户地址
     */
    public Map<String, Object> generateAddress() {
        Map<String, Object> address = new HashMap<>();
        address.put("Name", generateCustomerName());
        address.put("AddressLine1", generateStreetAddress());
        address.put("City", generateCity());
        address.put("StateOrRegion", generateState());
        address.put("PostalCode", generatePostalCode());
        address.put("CountryCode", "US");
        return address;
    }

    /**
     * 生成随机客户姓名
     */
    public String generateCustomerName() {
        String[] firstNames = {"John", "Jane", "Michael", "Sarah", "David", "Emily", "Robert", "Lisa", "James", "Mary"};
        String[] lastNames = {"Smith", "Johnson", "Williams", "Brown", "Jones", "Garcia", "Miller", "Davis", "Rodriguez", "Martinez"};
        return firstNames[random.nextInt(firstNames.length)] + " " + lastNames[random.nextInt(lastNames.length)];
    }

    /**
     * 生成随机街道地址
     */
    public String generateStreetAddress() {
        int number = random.nextInt(9999) + 1;
        String[] streets = {"Main St", "Oak Ave", "Pine Rd", "Elm St", "Maple Dr", "Cedar Ln", "Park Ave", "First St", "Second St", "Third St"};
        return number + " " + streets[random.nextInt(streets.length)];
    }

    /**
     * 生成随机城市
     */
    public String generateCity() {
        String[] cities = {"New York", "Los Angeles", "Chicago", "Houston", "Phoenix", "Philadelphia", "San Antonio", "San Diego", "Dallas", "San Jose"};
        return cities[random.nextInt(cities.length)];
    }

    /**
     * 生成随机州
     */
    public String generateState() {
        String[] states = {"CA", "TX", "FL", "NY", "PA", "IL", "OH", "GA", "NC", "MI"};
        return states[random.nextInt(states.length)];
    }

    /**
     * 生成随机邮政编码
     */
    public String generatePostalCode() {
        return String.format("%05d", random.nextInt(100000));
    }

    /**
     * 生成随机图片URL
     */
    public String generateImageUrl(String asin, int index) {
        return "https://mock-images.amazon.com/images/I/mock-image-" + asin + "-" + index + ".jpg";
    }

    /**
     * 生成随机销售排名
     */
    public int generateSalesRank() {
        return random.nextInt(1000000) + 1;
    }

    /**
     * 生成随机评分
     */
    public BigDecimal generateRating() {
        double rating = 1.0 + 4.0 * random.nextDouble(); // 1.0 - 5.0
        return BigDecimal.valueOf(rating).setScale(1, RoundingMode.HALF_UP);
    }

    /**
     * 生成随机评论数量
     */
    public int generateReviewCount() {
        return random.nextInt(10000);
    }

    /**
     * 生成随机库存状态
     */
    public String generateInventoryCondition() {
        String[] conditions = {"NewItem", "UsedLikeNew", "UsedVeryGood", "UsedGood", "UsedAcceptable", "CollectibleLikeNew"};
        return conditions[random.nextInt(conditions.length)];
    }

    /**
     * 生成随机布尔值
     */
    public boolean generateBoolean() {
        return random.nextBoolean();
    }

    /**
     * 生成随机百分比
     */
    public BigDecimal generatePercentage() {
        double percentage = random.nextDouble() * 100;
        return BigDecimal.valueOf(percentage).setScale(2, RoundingMode.HALF_UP);
    }

    /**
     * 从数组中随机选择元素
     */
    public <T> T randomChoice(T[] array) {
        return array[random.nextInt(array.length)];
    }

    /**
     * 从列表中随机选择元素
     */
    public <T> T randomChoice(List<T> list) {
        return list.get(random.nextInt(list.size()));
    }

    /**
     * 生成随机字符串
     */
    public String generateRandomString(int length) {
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < length; i++) {
            sb.append(chars.charAt(random.nextInt(chars.length())));
        }
        return sb.toString();
    }
}
